package com.xuecheng.utils;

import com.alibaba.fastjson.JSON;
import com.xuecheng.model.RestResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * http 工具类
 */
public class HttpUtil {
	
	public static void writerError(RestResponse restResponse, HttpServletResponse response) throws IOException {
		response.setContentType("application/json,charset=utf-8");
		response.setStatus(restResponse.getCode());
		JSON.writeJSONString(response.getOutputStream(), restResponse);
	}
	
	public static String getAccessToken(String ak, String sk) throws Exception {
		// 获取token地址
		String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
		String getAccessTokenUrl = authHost
			// 1. grant_type为固定参数
			+ "grant_type=client_credentials"
			// 2. 官网获取的 API Key
			+ "&client_id=" + ak
			// 3. 官网获取的 Secret Key
			+ "&client_secret=" + sk;
		URL realUrl = new URL(getAccessTokenUrl);
		// 打开和URL之间的连接
		HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
		connection.setRequestMethod("GET");
		connection.connect();
		// 获取所有响应头字段
            /*Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.err.println(key + "--->" + map.get(key));
            }*/
		// 定义 BufferedReader输入流来读取URL的响应
		BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		StringBuilder result = new StringBuilder();
		String line;
		while ((line = in.readLine()) != null) {
			result.append(line);
		}
		in.close();
		connection.disconnect();
		Map<String, Object> resultMap = JsonUtil.jsonToMap(result.toString());
		return resultMap.get("access_token").toString();
	}
	
	public static String post(String requestUrl, String accessToken, String params)
		throws Exception {
		String contentType = "application/x-www-form-urlencoded";
		return HttpUtil.post(requestUrl, accessToken, contentType, params);
	}
	
	public static String post(String requestUrl, String accessToken, String contentType, String params)
		throws Exception {
		String encoding = "UTF-8";
		if (requestUrl.contains("nlp")) {
			encoding = "GBK";
		}
		return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
	}
	
	public static String post(String requestUrl, String accessToken, String contentType, String params,
	                          String encoding)
		throws Exception {
		String url = requestUrl + "?access_token=" + accessToken;
		return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
	}
	
	public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
		throws Exception {
		URL url = new URL(generalUrl);
		// 打开和URL之间的连接
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		// 设置通用的请求属性
		connection.setRequestProperty("Content-Type", contentType);
		connection.setRequestProperty("Connection", "Keep-Alive");
		connection.setUseCaches(false);
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setConnectTimeout(20000);
		connection.setReadTimeout(20000);
		
		// 得到请求的输出流对象
		DataOutputStream out = new DataOutputStream(connection.getOutputStream());
		out.write(params.getBytes(encoding));
		out.flush();
		out.close();
		
		// 建立实际的连接
		connection.connect();
		// 获取所有响应头字段
		Map<String, List<String>> headers = connection.getHeaderFields();
		// 遍历所有的响应头字段
        /*for (String key : headers.keySet()) {
            System.err.println(key + "--->" + headers.get(key));
        }*/
		// 定义 BufferedReader输入流来读取URL的响应
		BufferedReader in = null;
		in = new BufferedReader(
			new InputStreamReader(connection.getInputStream(), encoding));
		StringBuilder result = new StringBuilder();
		String getLine;
		while ((getLine = in.readLine()) != null) {
			result.append(getLine);
		}
		in.close();
		connection.disconnect();
		//System.err.println("result:" + result);
		return result.toString();
	}
}
